package evil.wolf.business;

import evil.wolf.db.DatabaseManager;
import evil.wolf.excute.query.ExcuteQuery;
import evil.wolf.objects.ObjectNhapXuat;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;


public class BusinessNhap {
	
	DatabaseManager databaseManager;	
	ExcuteQuery exc;
	public int colCount;
	public String colName[];

	public BusinessNhap() {
		
		databaseManager = new DatabaseManager();
		exc = new ExcuteQuery();
	}
	
	
	public int getColumn(){
		return colCount;
	}
	
	
	public String[] getName(){
		return colName;
	}
	
	public List<ObjectNhapXuat> getListRecords() {
		DateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy");
//		dateFormatter.getDateInstance(DateFormat.MEDIUM);
		List<ObjectNhapXuat> rowList = new ArrayList<>();
		String [] caption = {"Số chứng từ", 
				"Ngày cấp", 
				"Khách hàng", 
				"Diễn giải", 
				"Loại chứng từ"};
		try {
			ResultSet rs = databaseManager.getResultSet(exc.selectNhap());
			ResultSetMetaData meta = rs.getMetaData();
			colCount = meta.getColumnCount();
			colName = new String[colCount];
			
			for (int h = 0; h < colCount; h++) {
//				colName[h] = meta.getColumnName(h + 1);
				colName[h] = caption[h];
			}
			
			while (rs.next()){
				
				ObjectNhapXuat newObjectNhap = new ObjectNhapXuat();
				
				newObjectNhap.setsoct(rs.getString(1));
//				Date date = rs.getDate(2);
//				String datestr = dateFormatter.format(date);
//				try {
//					date = (Date)dateFormatter.parse(datestr);
//				} catch (ParseException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
				newObjectNhap.setngayct(dateFormatter.format(rs.getDate(2)));
				newObjectNhap.setmakh(rs.getString(3));
//				newObjectNhap.setdg(rs.getString(4));
//				newObjectNhap.setloaict(rs.getString(5));
				
				rowList.add(newObjectNhap);
			}
			databaseManager.closeConnect();
		} catch (SQLException e) {

			JOptionPane.showMessageDialog(null,
					e.toString(), "ERROR", JOptionPane.INFORMATION_MESSAGE);
			e.printStackTrace();
		}
		return rowList;
	}
}
